//
//  CourseDetailCycleView.m
//  TeacherNet
//
//  Created by slz on 2017/11/15.
//  Copyright © 2017年 bnsr. All rights reserved.
//

#import "CourseDetailCycleView.h"
#import "CourseCycleModel.h"
#import "CourseCycleTableViewCell.h"

@implementation CourseDetailCycleView

-(instancetype)init{
    self = [super init];
    if (self){
        self.backgroundColor = [UIColor greenColor];
        [self initSubviews];
    }
    return self;
}

-(void)updateWithModelArray:(NSArray *)modelArray
{
    if (!modelArray || modelArray.count == 0) return;
    NSMutableArray * models = @[].mutableCopy;
    for (NSDictionary * dict in modelArray) {
        CourseCycleModel * model = [CourseCycleModel yy_modelWithDictionary:dict];
        [models addObject:model];
    }
    _modelArray = models;
    [_tableView reloadData];
}

- (void)initSubviews
{
    _tableView = [[YWTableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.bounces = NO;
    [self addSubview:_tableView];
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self);
    }];
}

#pragma mark - UITableViewDelegate && UITableViewDataSource

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * reuserIdentifier = @"CourseCycleTableViewCell";
    CourseCycleTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:reuserIdentifier];
    if (!cell){
        cell = [[CourseCycleTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuserIdentifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    CourseCycleModel * model = _modelArray[indexPath.section];
    CourseCycleModel * subModel = model.chapterCourseList[indexPath.row];
    cell.courseTitle.text = subModel.chapterTitle;
    cell.courseType.text = subModel.chapterStates;
    return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    CourseCycleModel * model = _modelArray[section];
    return model.chapterCourseList.count;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return _modelArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 75;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
        UIView * backView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 44)];
    
        
        UIImageView * imageView = [UIImageView new];
        imageView.image = [UIImage imageNamed:@"course_type_arrow"];
        imageView.size = CGSizeMake(22, 22);
        imageView.centerY = 22;
        imageView.left = 18;
        [backView addSubview:imageView];
        
        UILabel * titleLabel = [UILabel new];
        titleLabel.size = CGSizeMake(kScreenWidth - 60, 44);
        titleLabel.left = imageView.right + 5;
        titleLabel.top = 0;
        titleLabel.textColor = kDark_TextColor_Default;
        titleLabel.fontSize = 15;
        [backView addSubview:titleLabel];
        
        CourseCycleModel * subModel = _modelArray[section];
        
        titleLabel.text = subModel.chapterTitle;
        return backView;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return CGFLOAT_MIN;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
        return 44;
}


-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
}

@end
